<?php

class CpproductsController extends Core_Controller_Action 
{
	private $_api;
	private $_descript;
	
	public function init() {	
		$this->_api = new Products();	
		$this->_descript = new DescriptionProducts();
	}
	public function indexAction(){
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
		$langs = new Langs();
		$this->view->langs = $langs->getList();
	}
	public function addAction(){
		$langs = new Langs();
		$ln = $this->_getParam('ln');
		if($ln!='')
			$this->view->langs = $langs->getItem($ln);
		else
			$this->view->langs = $langs->getList();
		
		//lista kategorii
		$kat = new Category();
		$temp = $kat->getListWithName('pl');	
		$this->view->cat_list = array();
		foreach($temp as $one){
			$this->view->cat_list[$one['c_id']] = $one['title'];
		}
		
		$currency = new Currency();
		$this->view->currency = $currency->getList();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		if($this->_request->isPost())
		{
			$post = $this->_request->getPost();
			if($id)
			{
				$products = $this->_api->getItemDD( $id )->getFirst();
//				Core_Debug::pr($products->toArray());
//				exit;
			}
			else
			{
				$products = $this->_api;
				$help = $this->_api->getOrder()->getFirst();
				$products->order_num = $help->order_num + 1;
			}
			
			$products->size_s = $this->_getParam('size_s');
			$products->size_m = $this->_getParam('size_m');
			$products->size_l = $this->_getParam('size_l');
			$products->size_xl = $this->_getParam('size_xl');
			$products->size_xxl = $this->_getParam('size_xxl');
			$products->size_xxxl = $this->_getParam('size_xxxl');
			$products->category_c_id = $this->_getParam('category_c_id');

			$products->transport = $this->_getParam('transport');
			$prize = array();
			foreach ($this->view->currency as $one)
			{
				$prize[$one['name']] = $post['price'][$one['name']];	
				$prize[$one['name'].'_strike'] = $post['price'][$one['name'].'_strike'];			 
			}
			$products->price = serialize($prize);
			
			$products->save();
			
			//zapisywanie opisów produktów
			foreach ($this->view->langs as $one)
			{
				$dese = new DescriptionProducts();
				if($id)
					$des = $dese->getItemByProd( $id,$one['l_id'] )->getFirst();
				else
					$des = $dese;
				$des->products_p_id = $products->p_id;
				$des->langs_l_id = $one['l_id'];
				$des->title = $post['title'][$one['l_id']];
				$des->description = $post['description'][$one['l_id']];
				if($post['url'][$one['l_id']]!='')
					$des->url = $post['url'][$one['l_id']];
				else
					$des->url = $this->Seourl($post['title'][$one['l_id']]);
				$des->save();
			}
			$this->_redirect('cpproducts');
		}
		if($id)
		{
			$this->view->item = $this->_api->getItemDD( $id )->getFirst()->toArray();
			$prod_lang = array();
			foreach ($this->view->langs as $one)
			{
				$prod_lang[$one['l_id']] = $this->_descript->getItemByProd( $id,$one['l_id'] )->getFirst()->toArray();
			}
			$this->view->des = $prod_lang;
		}

	}
	public function deleteAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		if($id)
		{
			$this->_api->deleteItem($id);
			$this->_descript->deleteByProd($id);
		}
		$this->_redirect('cpproducts');
	}
	public function changeorderAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$where = intval( $this->_request->getParam( 'where', 0 ) );
		if($id)
		{
			if($where < 2){
				$product = $this->_api->getItem( $id )->getFirst();
				$help = $product->order_num;
				$order = $this->_api->getOrderItem( $help , $where )->getFirst();
				
				$product->order_num = $order->order_num;
				$product->save();
				
				$order->order_num = $help;
				$order->save();
			}else{
				$product = $this->_api->getItem( $id )->getFirst();
				$order = $this->_api->getOrderItem( $product->order_num , $where-2 )->getData();
				foreach ($order as $inny){
					$help = $product->order_num;
					$product->order_num = $inny->order_num;
					$inny->order_num = $help;
					$inny->save();
				}
				$product->save();
				//die(1);
			}
		}
		$this->_redirect('cpproducts');
	}
	
	public function addphotosAction() {
	
		require_once 'Image/ThumbLib.inc.php';
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$pid = intval( $this->_request->getParam( 'pid', 0 ) );
		
		$gl = intval( $this->_request->getParam( 'gl', 0 ) );
		
		$delete = intval( $this->_request->getParam( 'delete', 0 ) );
		
		$this->view->id = $id;
		
		$Gallery = new Gallery();
		
		if( $pid > 0 && $gl > 0 ) {
			
			$q = Doctrine_Manager::getInstance()->getCurrentConnection();
			$sql = 'UPDATE gallery SET main = 0 WHERE products_p_id = ' . $id;
			$conn = Doctrine_Manager::connection();
			$pdo = $conn->execute($sql);
			
			$it = $Gallery->getItem( $pid, $id )->getFirst();
			$it->main = 1;
			$it->save();
			$this->_redirect( 'cpproducts/addphotos/id/' . $id );
			exit;
		}
		
		if( $pid > 0 && $delete > 0 ) {
	
			$Gallery->deleteItem( $pid );
			$this->_redirect( 'cpproducts/addphotos/id/' . $id );
			exit;
		}
		
		
		$error = '';
	
		
		if( isSet( $_FILES['zdjecieId']['name'] ) && $id > 0 ) {
			$_FILES['zdjecieId']['name'] = md5( $_FILES['zdjecieId']['name'] . rand( 1, 1000000000 ) ). '.jpg';
			$adapter = new Zend_File_Transfer_Adapter_Http();

			$adapter->setDestination( $this->config['dirs']['upload'] );

			if (!$adapter->receive()) {
			    $messages = $adapter->getMessages();
			    $error = implode("\n", $messages);
			}
			
			$Gallery->image = $_FILES['zdjecieId']['name'];
			$Gallery->products_p_id = $id;
			$Gallery->save();
			
			$names = $adapter->getFileName();
			
			$thumb = PhpThumbFactory::create( $names );
			
			$t700 = preg_replace( '/(\.[a-zA-Z])/', '_big$0', $names );
			$thumb->resize(388, 388)->save( $t700 );
			
			$thumb2 = PhpThumbFactory::create( $names );
			$t200 = preg_replace( '/(\.[a-zA-Z])/', '_mid$0', $names );
			$thumb2->resize(190, 190)->save( $t200 );
			
			$thumb3 = PhpThumbFactory::create( $names );
			$t100 = preg_replace( '/(\.[a-zA-Z])/', '_list$0', $names );
			$thumb3->resize(190, 195)->save( $t100 );
			
			$thumb4 = PhpThumbFactory::create( $names );
			$t58 = preg_replace( '/(\.[a-zA-Z])/', '_small$0', $names );
			$thumb4->resize(50, 58)->save( $t58 );
			
			chmod( $names, 0777 );
			chmod( $t700,  0777 );	
			chmod( $t200,  0777 );
			chmod( $t100,  0777 );
			chmod( $t58,  0777 );
		}
		
		$this->view->photos = $Gallery->getList( $id );
	}
	
	public function uploadAction() {

		require_once 'Image/ThumbLib.inc.php';
		
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$error = '';
		
		$_FILES['zdjecieId']['name'] = md5( $_FILES['zdjecieId']['name'] . rand( 1, 1000000000 ) ). '.png';
		$adapter = new Zend_File_Transfer_Adapter_Http();
		$adapter->setDestination( $this->config['dirs']['upload'] );
		
		if (!$adapter->receive()) {
		    $messages = $adapter->getMessages();
		    $error = implode("\n", $messages);
		}
		
		$names = $adapter->getFileName();
		
		$thumb = PhpThumbFactory::create( $names );
		
		$t700 = preg_replace( '/(\.[a-zA-Z])/', '_700$0', $names );
		$thumb->resize(275, 375)->save( $t700 );
		
		$thumb2 = PhpThumbFactory::create( $names );
		$t200 = preg_replace( '/(\.[a-zA-Z])/', '_200$0', $names );
		$thumb2->resize(200, 250)->save( $t200 );
		
		$thumb3 = PhpThumbFactory::create( $names );
		$t100 = preg_replace( '/(\.[a-zA-Z])/', '_100$0', $names );
		$thumb3->resize(100, 130)->save( $t100 );
		
		chmod( $names, 0777 );
		chmod( $t700,  0777 );	
		chmod( $t200,  0777 );
		chmod( $t100,  0777 );
		
		
		echo '{ error : "'.$error.'", msg : "'.$_FILES['zdjecieId']['name'].'" }';
	}
	public function Seourl( $txt ) {

		$table = Array(
		//WIN
		"xb9" => "a", "xa5" => "A", "xe6" => "c", "xc6" => "C",
		"xea" => "e", "xca" => "E", "xb3" => "l", "xa3" => "L",
		"xf3" => "o", "xd3" => "O", "x9c" => "s", "x8c" => "S",
		"x9f" => "z", "xaf" => "Z", "xbf" => "z", "xac" => "Z",
		"xf1" => "n", "xd1" => "N",
		//UTF
		"xc4x85" => "a", "xc4x84" => "A", "xc4x87" => "c", "xc4x86" => "C",
		"xc4x99" => "e", "xc4x98" => "E", "xc5x82" => "l", "xc5x81" => "L",
		"xc3xb3" => "o", "xc3x93" => "O", "xc5x9b" => "s", "xc5x9a" => "S",
		"xc5xbc" => "z", "xc5xbb" => "Z", "xc5xba" => "z", "xc5xb9" => "Z",
		"xc5x84" => "n", "xc5x83" => "N",
		//ISO
		"xb1" => "a", "xa1" => "A", "xe6" => "c", "xc6" => "C",
		"xea" => "e", "xca" => "E", "xb3" => "l", "xa3" => "L",
		"xf3" => "o", "xd3" => "O", "xb6" => "s", "xa6" => "S",
		"xbc" => "z", "xac" => "Z", "xbf" => "z", "xaf" => "Z",
		"xf1" => "n", "xd1" => "N");
		
		$txt = strtr( $txt, $table );
		
		return str_replace( ' ', '-', $txt );
	}
}
	